package com.ymttest.database.sqlwapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ymt.core.tool.FormatUtil;
import com.ymt.database.SqlManager;
import com.ymttest.database.model.*;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.DataUtil.Random;

public class JUserWapper {
	SqlManager manage;

	public JUserWapper() {
		manage = new SqlManager("ymtuserdb");
	}

	/**
	 * 查询1000个seller用户 用于创建直播场景 可能出现
	 * 
	 * @return
	 */
	public List<Userinfo> select1KSeller() {
		List<Userinfo> userinfo = (List<Userinfo>) manage.getSelectResults("com.ymttest.database.mapping.JUserMapper.select1KSeller", null);
		manage.commit();
		return userinfo;
	}

	/**
	 * 查询新版java用户服务用户信息数据
	 * 
	 * @param userid
	 * @return
	 */
	public List<Userinfo> selectUserinfoByUid(Integer userid) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", userid);
		List<Userinfo> userinfo = (List<Userinfo>) manage.getSelectResults("com.ymttest.database.mapping.JUserMapper.selectUserinfoByUid", params);
		manage.commit();
		return userinfo;
	}

	/**
	 * 查询新版java用户服务用户信息详情数据
	 * 
	 * @param userid
	 * @return
	 */
	public List<Userinfoex> selectUserinfoexByUid(Integer userid) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", userid);
		List<Userinfoex> userinfo = (List<Userinfoex>) manage.getSelectResults("com.ymttest.database.mapping.JUserMapper.selectUserinfoexByUid", params);
		manage.commit();
		return userinfo;
	}

	/**
	 * 查询新版java用户服务第三方绑定
	 * 
	 * @param userid
	 * @return
	 */
	public List<ThirdpartyUserbind> selectthirdpartyuserbind(Integer userid) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", userid);
		List<ThirdpartyUserbind> userinfo = (List<ThirdpartyUserbind>) manage.getSelectResults("com.ymttest.database.mapping.JUserMapper.selectthirdpartyuserbind", params);
		manage.commit();
		return userinfo;
	}

	/**
	 * 查询新版java用户服务第三方绑定历史
	 * 
	 * @param userid
	 * @return
	 */
	public List<ThirdpartyUserbindHistory> selectthirdpartyuserbindhistory(Integer userid) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", userid);
		List<ThirdpartyUserbindHistory> userinfo = (List<ThirdpartyUserbindHistory>) manage
				.getSelectResults("com.ymttest.database.mapping.JUserMapper.selectthirdpartyuserbindhistory", params);
		manage.commit();
		return userinfo;
	}

	public void deleteUserinfoexByUid(Integer userid) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", userid);
		manage.delete("com.ymttest.database.mapping.JUserMapper.deleteUserinfoexByUid", params);
		manage.commit();
	}

	public void deleteUserinfoByUid(Integer userid) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", userid);
		manage.delete("com.ymttest.database.mapping.JUserMapper.deleteUserinfoByUid", params);
		manage.commit();
	}

	public void deleteUserinfoByEmail(String email) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("email", email);
		manage.delete("com.ymttest.database.mapping.JUserMapper.deleteUserinfoByEmail", params);
		manage.commit();
	}

	/**
	 * 获取一条用户信息
	 * 
	 * @return
	 */
	public Userinfo selectTop1Userinfo() {
		Userinfo model = new Userinfo();
		List<Userinfo> userinfolist = (List<Userinfo>) manage.getSelectResults("com.ymttest.database.mapping.JUserMapper.selectTop1Userinfo", null);
		manage.commit();
		if (userinfolist.size() > 0) {
			model = userinfolist.get(0);
		}
		return model;
	}

	/**
	 * 查询新版java用户服务用户-根据用户Id获取最新登录记录
	 * 
	 * @param userid
	 * @return
	 */
	public List<LoginLog> selectLastLoginLogByUid(Integer userid) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", userid);
		List<LoginLog> userinfo = (List<LoginLog>) manage.getSelectResults("com.ymttest.database.mapping.JUserMapper.selectLastLoginLogByUid", params);
		manage.commit();
		return userinfo;
	}

	public List<LoginLog> selectLastLoginLogByLoginId(String lid) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("lid", lid);
		List<LoginLog> userinfo = (List<LoginLog>) manage.getSelectResults("com.ymttest.database.mapping.JUserMapper.selectLastLoginLogByLoginId", params);
		manage.commit();
		return userinfo;
	}

	/**
	 * 查询新版java用户服务用户信息详情数据
	 * 
	 * @param userid
	 * @return
	 */
	public void setUserStateOfUserInfo(Integer userid, Integer userState) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", userid);
		params.put("userState", userState);
		manage.update("com.ymttest.database.mapping.JUserMapper.setUserStateOfUserInfo", params);
		manage.commit();
	}

	/**
	 * 删除uid对应的数据库手机绑定
	 * 
	 * @param userid
	 */
	public void removeMoblie(Integer userid) {
		manage.update("com.ymttest.database.mapping.JUserMapper.removeMoblie", userid);
		manage.commit();
	}

	/**
	 * 修改loginid 用于异常测试 历史存量数据
	 * 
	 * @param userid
	 * @param loginid
	 */
	public void updateLoginId(Integer userid, String loginid) {
		Map m = new HashMap<>();
		m.put("uid", userid);
		m.put("loginid", loginid);
		manage.update("com.ymttest.database.mapping.JUserMapper.updateLoginId", m);
		manage.commit();
	}

	/**
	 * 更新用户状态 -1-未激活（Inactive），0-正常（Normal），1-锁定（Locked），2-已注销（Close）
	 * 
	 * @param userid
	 * @param userstatus
	 */
	public void updateUserState(Integer userid, Integer userstate) {
		Map m = new HashMap<>();
		m.put("uid", userid);
		m.put("userstate", userstate);
		manage.update("com.ymttest.database.mapping.JUserMapper.updateUserState", m);
		manage.commit();
	}

	public void setOpenIdOfThirdPartyUserBind(String thirdPartyId, String openId) {
		Map<String, Object> params = new HashMap<String, Object>();

		params.put("thirdPartyId", thirdPartyId);
		params.put("openId", openId);
		manage.update("com.ymttest.database.mapping.JUserMapper.setOpenIdOfThirdPartyUserBind", params);
		manage.commit();
	}

	public List<UserAddress> selectAddress(Integer uid, String detailaddress) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", uid);
		params.put("detailaddress", detailaddress);
		List<UserAddress> useradd = manage.getSelectResults("com.ymttest.database.mapping.JUserMapper.selectAddress", params);
		manage.commit();
		return useradd;
	}

	/**
	 * 查询微信用户信息
	 * 
	 * @param userid
	 * @return
	 */
	public ThirdpartyUserbind selectWXUser() {
		List<ThirdpartyUserbind> list = manage.getSelectResults("com.ymttest.database.mapping.JUserMapper.selectWXUser", null);
		ThirdpartyUserbind userinfo = list.get(Random.getRandomIntFromRange(0, list.size() - 1));
		manage.commit();
		return userinfo;
	}

	/**
	 * 查询微信用户映射 im用 记录uid和微信token关系
	 * 
	 * @param uid
	 * @return
	 */
	public List<Map> selectWxUserMapping(Integer uid) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", uid);
		List<Map> m = (List<Map>) manage.getSelectResults("com.ymttest.database.mapping.JUserMapper.selectWxUserMapping", params);
		manage.commit();
		return m;
	}

	public List<Map> selectUsermark(Integer uid) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", uid);
		List<Map> m = (List<Map>) manage.getSelectResults("com.ymttest.database.mapping.JUserMapper.selectUsermark", params);
		manage.commit();
		return m;
	}

	public List<UserinfoUpdaterecord> selectUserinfoUpdaterecord(Integer uid) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", uid);
		List<UserinfoUpdaterecord> m = (List<UserinfoUpdaterecord>) manage.getSelectResults("com.ymttest.database.mapping.JUserMapper.selectUserinfoUpdaterecord", params);
		manage.commit();
		return m;
	}

	public Userinfo selectUserinfoByLevel(Integer levelId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("UserLevel", levelId);
		List<Userinfo> userinfo = (List<Userinfo>) manage.getSelectResults("com.ymttest.database.mapping.JUserMapper.selectUserinfoByLevel", params);
		manage.commit();
		return userinfo.get(0);
	}

	/**
	 * 关闭数据库连接
	 */
	public void close() {
		manage.close();
	}

	@Override
	protected void finalize() throws Throwable {
		this.close();
		manage = null;
		super.finalize();
	}

	public static void main(String[] args) {
		// new JUserWapper().updateLoginId(30163320, "130163320");
		System.out.println(new JUserWapper().selectUserinfoByLevel(2).getUserId());
		// System.out.println(FormatUtil.beanToGSONString(user));
		// List<Userinfoex> userex= new JUserWapper().selectUserinfoexByUid(4);
		// System.out.println(FormatUtil.beanToGSONString(userex));
		// new JUserWapper().removeMoblie(416532);
	}

}
